package com.weddingee.action.home;

import com.weddingee.action.base.BaseAction;
import com.weddingee.model.User;
import com.weddingee.dao.base.BaseDao;
import com.weddingee.dao.home.NewsDao;
import com.weddingee.dto.ViewDto;
import com.weddingee.util.DaoFactory;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import com.weddingee.model.News;
import java.util.Date;

public class NewsAction extends BaseAction {

	private NewsDao newsDao = (NewsDao) DaoFactory.create(2);
	private Long newsId;
	private String newsname;
	private Date createTime;
	private String headpic;
	private String message;
	private String absMessage;
	private int page;
	private List<News> newsList;
	private ViewDto view = new ViewDto();
	private int totalPage;
	private News news;

	// add
	public String add() {
		News news = new News();
		news.setCreateTime(new Date());
		news.setName(newsname);
		news.setMessage(message);
		news.setAbsMessage(absMessage);
		news.setPicurl(headpic);
		newsDao.save(news);
		view.setData(news);
		return SUCCESS;
	}

	// list
	public String list() {
		try {
			if (page == 0) {
				page = 1;
			}
			newsList = newsDao.listNews(page);
			int totalCount = newsDao.listNewsTotalCount();

			if (totalCount % 10 != 0) {
				totalPage = totalCount / 10 + 1;
			} else {
				totalPage = totalCount / 10;
			}

			return SUCCESS;
		} catch (Exception e) {
			log.error("2222", e);
			return ERROR;
		}
	}

	// find
	public String find() {
		try {
			News news = newsDao.find(newsId);
			view.setData(news);
			return SUCCESS;
		} catch (Exception e) {
			return ERROR;
		}
	}

	// update
	public String update() {
		News news = newsDao.find(newsId);
		news.setCreateTime(new Date());
		if (newsname != null) {
			news.setName(newsname);
		}
		if (message != null) {
			news.setMessage(message);
		}
		if (headpic != null) {
			news.setPicurl(headpic);
		}
		news.setAbsMessage(absMessage);
		newsDao.update(news);
		view.setData(news);
		return SUCCESS;
	}

	// delete
	public String delete() {
		newsDao.delete(newsId);
		return SUCCESS;
	}

	public String detail() {
		try {
			if (null == newsId) {
				log.error("news id can not be null.");
				return ERROR;
			}
			news = newsDao.find(newsId);
			return SUCCESS;
		} catch (Exception e) {
			log.error("detail error.", e);
			return ERROR;
		}
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setNewsDao(NewsDao newsDao) {
		this.newsDao = newsDao;
	}

	public Long getNewsId() {
		return newsId;
	}

	public void setNewsId(Long newsId) {
		this.newsId = newsId;
	}

	public String getNewsname() {
		return newsname;
	}

	public void setNewsname(String newsname) {
		this.newsname = newsname;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public String getHeadpic() {
		return headpic;
	}

	public void setHeadpic(String headpic) {
		this.headpic = headpic;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<News> getNewsList() {
		return newsList;
	}

	public ViewDto getView() {
		return view;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public News getNews() {
		return news;
	}

	public String getAbsMessage() {
		return absMessage;
	}

	public void setAbsMessage(String absMessage) {
		this.absMessage = absMessage;
	}

}
